package hk.m4s.lr.repair.test.ui.user;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.NoticeModel;
import hk.m4s.lr.repair.test.model.NoticeModelResult;
import hk.m4s.lr.repair.test.service.notice.NoticeService;
import hk.m4s.lr.repair.test.ui.adapter.UserNoticeAdapter;
import hk.m4s.lr.repair.test.ui.webview.WebViewActivity;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

public class UserNoticeActivity extends UeBaseActivity implements VListView.IXListViewListener {
	/** x下拉列表 */
	private VListView vlist;
	private List<NoticeModel> newsList=new ArrayList<>();
	private UserNoticeAdapter adapter;
	private TextView tips_text;
	/** Handler对象 */
	private Handler handler;
	/** 最后一条记录的id，0表示没有获取过 */
	private String lastId = "0";
    Context context;
	private int pageNum = 1;
	private int pageSize = 10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		baseSetContentView(R.layout.activity_user_notice);
		hiddenFooter();
		showGoBackBtn();
		setTitleText("系统消息");
		context=this;
		tips_text=(TextView)findViewById(R.id.tips_text);
		vlist=(VListView) findViewById(R.id.vlist);
		vlist.setXListViewListener(this);
		adapter=new UserNoticeAdapter(UserNoticeActivity.this,newsList);
		vlist.setAdapter(adapter);
        vlist.setOnItemClickListener(clickItem);
		onLoad();
		getData("1");
	}
	private void onLoad() {
		vlist.stopRefresh();
		vlist.stopLoadMore();
		Date data = new Date(System.currentTimeMillis());
		vlist.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA).format(data));
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		 getData("1");
		vlist.setPullLoadEnable(true);
		pageNum=0;
	}

	@Override
	protected void onResume() {
		super.onResume();
		getData("1");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		vlist.setPullRefreshEnable(true);
		pageNum++;
		getData("2");
	}
	AdapterView.OnItemClickListener clickItem = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
								long arg3) {
			// TODO Auto-generated method stub
			NoticeModel model=newsList.get(arg2-1);
			if(!(model.getReadTime()==null||model.getReadTime().equals(""))){
			}else {
				getSysNoticeRead(model.getId());
			}
			Intent aboutIntent=new Intent(UserNoticeActivity.this, WebViewActivity.class);
			aboutIntent.putExtra("title","系统消息");
			aboutIntent.putExtra("show","2");
			aboutIntent.putExtra("url",model.getHtmlUrl());
			startActivity(aboutIntent);
		}

	};
	public void getData(final String refreshType){
		Map<String, Object> parmas = new HashMap<>();
		parmas.put("device_type", Constant.device_type);
		parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
		try {
			JSONObject jsondata = new JSONObject();
			jsondata.put("pageNum", pageNum);
			jsondata.put("pageSize", pageSize);
			jsondata.put("type", "4");
			jsondata.put("clientID", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
			parmas.put("jsonText",jsondata.toString());
		}catch (JSONException e){

		}
		NoticeService.sysNoticeList(context,parmas , new ResponseCallback<NoticeModelResult>() {
			@Override
			public void onSuccess(NoticeModelResult deviceModelResult) {
				onLoad();
				if(deviceModelResult.getList()!=null) {
					tips_text.setVisibility(View.GONE);
					switch (refreshType) {
						case "1":
							newsList.clear();
							newsList.addAll(deviceModelResult.getList());
							if (deviceModelResult.getList().size() <= pageSize) {
								vlist.removeFooterView();
								vlist.setPullLoadEnable(false);
							} else {
								vlist.showFooterView();
								vlist.setPullLoadEnable(true);
							}
							break;
						case "2":
							newsList.addAll(deviceModelResult.getList());
							if (deviceModelResult.getList().size() <= pageSize) {
								vlist.removeFooterView();
								vlist.setPullLoadEnable(false);
							}
							break;
					}
					adapter.notifyDataSetChanged();
				}else{
					tips_text.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onError(Response response, int errorCode, String errorMsg, Exception e) {

			}
		});
	}
	public void getSysNoticeRead(final String id){
		Map<String, Object> parmas = new HashMap<>();
		parmas.put("device_type", Constant.device_type);
		parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
		try {
			JSONObject jsondata = new JSONObject();
			jsondata.put("id", id);
			parmas.put("jsonText",jsondata.toString());
		}catch (JSONException e){

		}
		NoticeService.sysNoticeRead(context,parmas , new ResponseCallback<NoticeModelResult>() {
			@Override
			public void onSuccess(NoticeModelResult deviceModelResult) {


			}

			@Override
			public void onError(Response response, int errorCode, String errorMsg, Exception e) {

			}
		});
	}
}
